use crate::libs::lazy_tera::TERA;
use actix_web::{middleware, web, App,error,HttpRequest,Error, HttpServer,HttpResponse, Result,};

use crate::schema;
use crate::models;
use diesel;
use diesel::*;
use diesel::{r2d2::ConnectionManager,
r2d2::PooledConnection,
MysqlConnection,
connection::SimpleConnection,
connection::Connection};


fn establish_connection() -> MysqlConnection {
    MysqlConnection::establish(&"mysql://xxx").expect(" url is wrong")
}

//type Conn = PooledConnection<ConnectionManager<MysqlConnection>>;
//type Pool = r2d2::Pool<ConnectionManager<MysqlConnection>>;

//pub fn index(req: HttpRequest,pool: web::Data<Pool>) -> Result<HttpResponse, Error> {
pub fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
    use schema::users::dsl::*;
    let connection = establish_connection();
    let  items = users.filter(id.eq(&1)).load::<models::user::User>(&connection).expect("worng");
    println!("{}",items[0].name);
    let mut ctx=tera::Context::new();
    ctx.insert("name",&items[0].name);
    //ctx.insert("name",&"lm");
    ctx.insert("age", &20);
    let s =TERA.render("index.html",&ctx).map_err(|e| error::ErrorInternalServerError(e))?;
    Ok(HttpResponse::Ok().content_type("text/html").body(s))

}